using System.Threading;
using Echo.Context.UltiProNet.Contexts;
using Echo.Core.TestAbstraction.WebUITest.MbUnit;
using Echo.Models.UltiProNet;
using Echo.Models.UltimateSoftware;
using MbUnit.Framework;

////*************************************************************************************************************************************
////Created by: Dionny Santiago
////Date Created: March 30, 2012 
////Project: ASP.NET Payroll Models Smoke Test
////Test Cases: 9
////*************************************************************************************************************************************

namespace Echo.Automation.Payroll
{
    using Echo.Core.Common;

    [TestFixture]
    [Header("Config")]
    [Config(".NET")]
    [Author("Dionny Santiago", "dionny_santiago@ultimatesoftware.com")]
    [Category("Payroll Models")]
    [Category("BADSmokeTest")]
    public class AddPayrollModel : AutomatedTest
    {
        UltiPro UltiPro;
        UltiProDatabaseContext Database;

        [FixtureSetUp]
        public void SetUp()
        {
            UltiPro = Launch<UltiPro>();

            Database = Init<UltiProDatabaseContext>();
            Database.Company = ConfigDatabase("ULTIPRO_CALENDAR");
            
            Database.Company.AddRollBack("paygroupmodel", "pgmPayGroup ='QUWK4' OR pgmPayGroup ='QUWK6' OR pgmPayGroup ='QUWK1'");
            Database.Company.AddRollBack("PayGroup", "pgrPayGroup = 'QUWK1'");

            Database.Company.Execute("delete from paygroupmodel Where pgmPayGroup ='QUWK4'");
            Database.Company.Execute("update paygroupmodel set pgmpayrollmodelid = 500000 WHERE pgmPayGroup ='QUWK6'");
            Database.Company.Execute("Update PayGroup Set PgrIsPayRollOpen = 'N' where pgrPayGroup = 'QUWK1'");
            Database.Company.Execute("delete p from copayrollprocessclusters p inner join copayrollmodelprocesses on ppcPayrollModelProcessID = pmpPayrollModelProcessID inner join copayrollmodel on prmPayrollModelID = pmpPayrollModelID where prmPayrollModelCode = 'smoke'");
            Database.Company.Execute("delete p from copayrollmodelprocesses p inner join copayrollmodel on prmPayrollModelID = pmpPayrollModelID where prmPayrollModelCode = 'smoke'");
            Database.Company.Execute("delete pg from PayGroupModel pg join CoPayrollModel pr on pr.prmPayrollModelID = pg.pgmPayrollModelID where  pr.prmPayrollModelCode = 'SMOKE'");
            Database.Company.Execute("delete from copayrollmodel where prmPayrollModelCode = 'SMOKE'");

            // Navigation to Payroll Model Setup
            UltiPro.Pages.Login.LoginAs("atnipj", "password", Config.LanguageCode);
            UltiPro.Menu.PayrollAdmin.Click(link => link.PayrollModels);
        }

        [FixtureTearDown]
        public void TearDown()
        {
            UltiPro.Browser.Quit();

            Database.Company.RollBackDatabase();

            Database.Company.Execute("delete p from copayrollprocessclusters p inner join copayrollmodelprocesses on ppcPayrollModelProcessID = pmpPayrollModelProcessID inner join copayrollmodel on prmPayrollModelID = pmpPayrollModelID where prmPayrollModelCode = 'smoke'");
            Database.Company.Execute("delete p from copayrollmodelprocesses p inner join copayrollmodel on prmPayrollModelID = pmpPayrollModelID where prmPayrollModelCode = 'smoke'");
            Database.Company.Execute("delete pg from PayGroupModel pg join CoPayrollModel pr on pr.prmPayrollModelID = pg.pgmPayrollModelID where  pr.prmPayrollModelCode = 'SMOKE'");
            Database.Company.Execute("delete from copayrollmodel where prmPayrollModelCode = 'SMOKE'");
        }

        [Test(Order = 1)]
        public void Test_Payroll_Models_Summary_Header()
        {
            UltiPro.Pages.ModelSetupSummary.Is(control => control.PageHeaderLabel, "innerHtml", "Payroll Models Summary");
        }

        [Test(Order = 2)]
        public void Test_Add_Payroll_Model()
        {
            // Add a new model - Model Details page
            UltiPro.Pages.ModelSetupSummary.ButtonBar.Click(button => button.Add);
            UltiPro.Browser.SwitchToWindow("Add/Change Payroll Models");

            UltiPro.Pages.ModelSetupWizardStart.Is(control => control.PageHeaderLabel, "innerHtml", UltiPro.Localization.Strings.Model_Details.ToTitleCase());
            UltiPro.Pages.ModelSetupWizardStart.Set(control => control.CodeTextBox, "SMOKE");
            UltiPro.Pages.ModelSetupWizardStart.DescriptionTextBox.Set(control => control.EnglishTextBox, "A smoke test for Everest!@#$%^&*()'");
            UltiPro.Pages.ModelSetupWizardStart.ButtonBar.Click(control => control.Next);

            // Choose Steps/Reports page - base the new model on system delivered models
            UltiPro.Pages.ModelSetupWizardSteps.Is(control => control.PageHeaderLabel, "innerHtml", "Choose Steps/Reports");
            UltiPro.Pages.ModelSetupWizardSteps.Exists(control => control.ModelTypesSelect);
            UltiPro.Pages.ModelSetupWizardSteps.Exists(control => control.BaseModelOnSelect);
            UltiPro.Pages.ModelSetupWizardSteps.Set(control => control.ModelTypesSelect, "1");
            UltiPro.Pages.ModelSetupWizardSteps.Set(control => control.BaseModelOnSelect, "BASESYS");

            // Move cluster from right to left (remove both NACHA and Tip Allocation)
            UltiPro.Pages.ModelSetupWizardSteps.SelectedStepExists("Create direct deposit file for transmission");
            //UltiPro.Pages.ModelSetupWizardSteps.Click(list => list.SelectedListControl);
            UltiPro.Pages.ModelSetupWizardSteps.ClickSelectedStepByName("Create direct deposit file for transmission");
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.MoveLeftButton);
            UltiPro.Pages.ModelSetupWizardSteps.ClickSelectedStepByName("Allocate tips");
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.MoveLeftButton);

            // Confirm steps are no longer selected
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.CategoryAllLink);
            //UltiPro.Pages.ModelSetupWizardSteps.Click(list => list.SelectedListControl);
            UltiPro.Pages.ModelSetupWizardSteps.AvailableStepExists("Create direct deposit file for transmission");
            UltiPro.Pages.ModelSetupWizardSteps.AvailableStepExists("Allocate tips");

            // Move cluster from left to right (NACHA added back, and Run Pre-Check Summary Disbursement Listing)
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.CategoryPrintLink);
            //UltiPro.Pages.ModelSetupWizardSteps.Click(list => list.SelectedListControl);
            UltiPro.Pages.ModelSetupWizardSteps.ClickAvailableStepByName("Create direct deposit file for transmission");
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.MoveRightButton);
            UltiPro.Pages.ModelSetupWizardSteps.SelectedStepExists("Close and complete payroll for this period");
            UltiPro.Pages.ModelSetupWizardSteps.ClickSelectedStepByName("Close and complete payroll for this period");
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.CategoryReportsLink);

            // Echo workaround to scroll item into view.  Waits for focus on the reports category and then scrolls to the bottom of the scrollable list.
            // Firefox driver issue "The element is not visible so it may not be interacted with."
            UltiPro.Pages.ModelSetupWizardSteps.Is(control => control.CategoryReportsLink.ToJQuery().parent(), "class", "selectedCategory");
            UltiPro.Pages.ModelSetupWizardSteps.PressKeyboardKey(list => list.AvailableItemLinkByIndex("3"), KeyboardKey.End);

            // Clicks "Run the Pre-Check Summary Liability report".
            UltiPro.Pages.ModelSetupWizardSteps.Click(span => span.AvailableItemLinkByIndex("11"));
            UltiPro.Pages.ModelSetupWizardSteps.Click(control => control.MoveRightButton);
            UltiPro.Pages.ModelSetupWizardSteps.ButtonBar.Click(control => control.Next);

            // Ensure no exceptions occurred during service post (ULTI-105235).
            UltiPro.Alert.NotExists();

            // Name Process page - edit #2 process name
            UltiPro.Pages.ModelSetupWizardProcesses.Is(control => control.PageHeaderLabel, "innerHtml", "Name Processes");
            UltiPro.Pages.ModelSetupWizardProcesses.Set(control => control.SecondProcessTextBox, "SWAT Edit-Create and calculate payroll");
            UltiPro.Pages.ModelSetupWizardProcesses.ButtonBar.Click(control => control.Next);

            // Assign pay groups page - assign model to QUWK4 and QUWK1
            UltiPro.Pages.ModelSetupWizardPayGroups.Is(control => control.PageHeaderLabel, "innerHtml", "Assign Pay Groups");
            UltiPro.Pages.ModelSetupWizardPayGroups.Set(control => control.RegularPayrollAvailablePayGroupsSelect, "QUWK4");
            UltiPro.Pages.ModelSetupWizardPayGroups.Click(control => control.RegularPayrollMoveRight);
            UltiPro.Pages.ModelSetupWizardPayGroups.Set(control => control.RegularPayrollAvailablePayGroupsSelect, "QUWK1");
            UltiPro.Pages.ModelSetupWizardPayGroups.Click(control => control.RegularPayrollMoveRight);
            UltiPro.Pages.ModelSetupWizardPayGroups.ButtonBar.Click(control => control.Next);

            // Summary page
            UltiPro.Pages.AddChangePayrollModelsSubmitProcess.ButtonBar.Exists(control => control.Save, 120);
            UltiPro.Pages.AddChangePayrollModelsSubmitProcess.ButtonBar.Click(control => control.Save);
        }

        [Test(Order = 3)]
        public void Confirm_SMOKE_model_was_added()
        {
            Database.Company.Verify("select count(*) from CoPayrollModel where prmPayrollModelCode = 'SMOKE'",
                "Payroll_Smoke_Test_01.xml", 240);
            Database.Company.Verify("select prmIsSystem, prmIsVisible, prmPayrollModelCode, prmPayrollModelDesc, prmPayrollModelNotes from CoPayrollModel where prmPayrollModelCode = 'SMOKE'",
                "Payroll_Smoke_Test_02.xml", 240);
        }

        [Test(Order = 4)]
        public void Confirm_SMOKE_model_child_records_were_added_to_PayGroup()
        {
            Database.Company.Verify("select * FROM (select CASE when count(*) = 4 then 1 else 0 end as PGMCount from PayGroupModel inner join CoPayrollModel on pgmPayrollModelID = prmPayrollModelID WHERE prmPayrollModelCode = 'SMOKE') AS INNERSELECT WHERE PGMCount = 1",
                "Payroll_Smoke_Test_03.xml", 240);
            Database.Company.Verify("select pgmPayGroup, pgmPayrollType from PayGroupModel inner join CoPayrollModel on pgmPayrollModelID = prmPayrollModelID WHERE prmPayrollModelCode = 'SMOKE' Order By pgmPayGroup,pgmPayrollType",
                "Payroll_Smoke_Test_04.xml", 240);
        }

        [Test(Order = 5)]
        public void Confirm_SMOKE_model_child_records_were_added_to_PayrollModel_and_Cluster_tables()
        {
            Database.Company.Verify("select * FROM (select CASE when count(ppcClusterId) = 21 then 1 else 0 end as ClusterCount From vwPayrollModel With (nolock) Join vwPayrollModelProcesses With (nolock) On pmpPayrollModelID = prmPayrollModelID Join vwPayrollProcessClusters With (nolock) On ppcPayrollModelProcessId = pmpPayrollModelProcessID Left Join jmsJobDefSource js With (nolock) On js.DPMProcessDefinition = pmpDPMProcessID Left Join jmsJobDef jd with (nolock) On jd.JobDefCode = js.JobDefCode Where prmPayrollModelCode = 'SMOKE') AS INNERSELECT WHERE ClusterCount = 1",
                "Payroll_Smoke_Test_05.xml", 240);
            Database.Company.Verify("Select prmPayrollModelDesc,pmpDesc, jd.jobDefDescription,ppcClusterId,ppcClusterOrder From vwPayrollModel With (nolock) Join vwPayrollModelProcesses With (nolock) On pmpPayrollModelID = prmPayrollModelID Join vwPayrollProcessClusters With (nolock) On ppcPayrollModelProcessId = pmpPayrollModelProcessID Left Join jmsJobDefSource js With (nolock) On js.DPMProcessDefinition = pmpDPMProcessID Left Join jmsJobDef jd with (nolock) On jd.JobDefCode = js.JobDefCode Where prmPayrollModelCode = 'SMOKE' Order By pmpProcessOrder, ppcClusterOrder",
                "Payroll_Smoke_Test_06.xml", 240);
        }

        [Test(Order = 6)]
        public void Confirm_new_model_was_assigned_to_pay_groups_in_PayGroupModel_table()
        {
            Database.Company.Verify("select * FROM (select CASE when count(pgmPaygroup) = 4 then 1 else 0 end as ClusterCount from paygroupmodel	inner join copayrollmodel on pgmPayrollModelID = prmPayrollModelID where prmpayrollmodelcode = 'SMOKE' ) AS INNERSELECT WHERE ClusterCount = 1",
                "Payroll_Smoke_Test_07.xml", 240);
            Database.Company.Verify("select pgmPaygroup,pgmPayrollType from paygroupmodel	inner join copayrollmodel on pgmPayrollModelID = prmPayrollModelID where prmpayrollmodelcode = 'SMOKE' order by pgmpaygroup desc, pgmpayrolltype asc",
                "Payroll_Smoke_Test_08.xml", 240);
            // Cleanup
            Database.Company.Execute("Update PaygroupModel Set pgmPayrollModelID = 500004 Where pgmPayGroup ='QUWK4'");
            Database.Company.Execute("Update PaygroupModel Set pgmPayrollModelID = 500001 Where pgmPayGroup ='QUWK1'");
        }

        [Test(Order = 7)]
        public void Filter_and_delete_SMOKE_model()
        {
            UltiPro.Browser.SwitchToWindow("Atnip");
            UltiPro.Menu.PayrollAdmin.Click(link => link.PayrollModels);

            UltiPro.Pages.ModelSetupSummary.ModelGrid.Find(grid => grid.Description, FilterType.Contains, "A smoke");
            UltiPro.Pages.ModelSetupSummary.ModelGrid.Row(grid => grid.ModelDescription, "A smoke test for Everest!@#$%^&*()'").Click(control => control.DeleteCheckBox);
            UltiPro.Pages.ModelSetupSummary.ButtonBar.Click(control => control.Delete);
            UltiPro.Alert.Accept();

            Thread.Sleep(5000);
        }

        [Test(Order = 8)]
        public void Confirm_SMOKE_model_was_deleted()
        {
            Database.Company.Verify("select count(*) as ModelCount from CoPayrollModel where prmPayrollModelCode = 'SMOKE'",
                "Payroll_Smoke_Test_09.xml", 240);
            Database.Company.Verify("Select count(*) as ClusterCount from copayrollmodel With (nolock) Join PaygroupModel With (nolock) On PgmPayrollModelID = prmPayrollModelID Join vwPayrollModelProcesses With (nolock) On pmpPayrollModelID = prmPayrollModelID Join vwPayrollProcessClusters With (nolock) On ppcPayrollModelProcessId = pmpPayrollModelProcessID Left Join jmsJobDefSource js With (nolock) On js.DPMProcessDefinition = pmpDPMProcessID Left Join jmsJobDef jd with (nolock) On jd.JobDefCode = js.JobDefCode Where prmPayrollModelCode = 'SMOKE'",
                "Payroll_Smoke_Test_10.xml", 240);
        }

        [Test(Order = 9)]
        public void Confirm_SMOKE_model_children_tables()
        {
            Database.Company.Verify("select pgmPaygroup,pgmPayrollModelID,pgmPayrollType from PayGroupModel where pgmPayGroup in ('QUWK4', 'QUWK1') order by pgmpaygroup desc, pgmpayrolltype asc",
                "Payroll_Smoke_Test_11.xml", 240);
        }
    }
}
